//
//  MainControll.m
//  MBObjectServer
//
//  Created by admin on 4/4/14.
//  Copyright (c) 2014 MBpayer. All rights reserved.
//

#import "MainControll.h"

@implementation MainControll
@synthesize baseserver;

-(id)init{
    self=[super init];
    if (!self) {
        return nil;
    }
    baseserver = [[MBBaseServer alloc]init];
    return self;
}

#pragma mark  获取当班金额记录(GetMoneyOnDuty)
-(void)GetMoneyOnDuty:(MainReceiveSuccess)processSuccess{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * cashboxdic = [[NSMutableDictionary alloc] init];
    NSNumber * casherTeamId = [NSNumber numberWithInt:[[userDefaults objectForKey:kCashierTeamID] intValue]];
    [cashboxdic setObject:[userDefaults objectForKey:kOrgCode] forKey:kOrgCode];
    [cashboxdic setObject:casherTeamId forKey:kCashierTeamID];
    __weak MainControll * weakSelf = self;
    [self.baseserver setRequestStyle:nil andSetPathString:@"cashbox_handler.ashx" andSetHeadString:nil andActionName:@"GetMoneyOnDuty" andSetMainDictionary:cashboxdic block:^(NSDictionary *responseData) {
        NSDictionary * dic = responseData;
        BOOL  Result = [[dic objectForKey:@"Result"] boolValue];
        
        if (!Result) {
            NSString * ErrMessage = [dic objectForKey:@"ErrMessage"];
            UIAlertView * alertview = [[UIAlertView alloc] initWithTitle:@"错误" message:ErrMessage delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertview show];
        }
        else{
            [weakSelf saveMoneyOnDuty:dic];
            [weakSelf offDutyFlow:^(BOOL flag) {
                processSuccess(flag);
            }];
        }
    } failblock:^(NSError *error) {
        
    }];
    
}
#pragma mark 交班(OffDutyFlow)
-(void)offDutyFlow:(MainReceiveSuccess)processSuccess{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * cashboxdic = [[NSMutableDictionary alloc] init];
    NSNumber * casherTeamId = [NSNumber numberWithInt:[[userDefaults objectForKey:kCashierTeamID] intValue]];
    NSNumber * cashierid = [NSNumber numberWithInt:[[userDefaults objectForKey:kCashierID] intValue]];
    NSNumber * cashvalue = [NSNumber numberWithInt:[[userDefaults objectForKey:kCashSale] intValue]];
    NSNumber * cardvalue = [NSNumber numberWithInt:[[userDefaults objectForKey:kCardSale] intValue]];
    NSNumber * VoucherValue = [NSNumber numberWithInt:[[userDefaults objectForKey:kVoucherSale] intValue]];
    NSNumber * CashVoucherValue = [NSNumber numberWithInt:[[userDefaults objectForKey:kVoucher2Sale] intValue]];
    NSNumber * OtherValue = [NSNumber numberWithInt:[[userDefaults objectForKey:kOtherSale] intValue]];
    NSNumber * Memo = [NSNumber numberWithInt:[[userDefaults objectForKey:kSumTotal] intValue]];
    
    [cashboxdic setObject:[userDefaults objectForKey:kOrgCode] forKey:kOrgCode];
    [cashboxdic setObject:cashierid forKey:kCashierID];
    [cashboxdic setObject:casherTeamId forKey:kCashierTeamID];
    [cashboxdic setObject:cashvalue forKey:@"CashValue"];
    [cashboxdic setObject:cardvalue forKey:@"CardValue"];
    [cashboxdic setObject:VoucherValue forKey:@"VoucherValue"];
    [cashboxdic setObject:CashVoucherValue forKey:@"CashVoucherValue"];
    [cashboxdic setObject:OtherValue forKey:@"OtherValue"];
    [cashboxdic setObject:Memo forKey:@"Memo"];
    
    [self.baseserver setRequestStyle:nil andSetPathString:@"cashbox_handler.ashx" andSetHeadString:nil andActionName:@"OffDutyFlow" andSetMainDictionary:cashboxdic block:^(NSDictionary *responseData) {
        NSDictionary * dic = responseData;
        BOOL  Result = [[dic objectForKey:@"Result"] boolValue];
        
        if (!Result) {
            NSString * ErrMessage = [dic objectForKey:@"ErrMessage"];
            UIAlertView * alertview = [[UIAlertView alloc] initWithTitle:@"错误" message:ErrMessage delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertview show];
        }
        else{
            //将上述数据全部存储到NSUserDefaults中
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            //
            [userDefaults setObject:nil forKey:kUserID];
            processSuccess(Result);
        }

    } failblock:^(NSError *error) {
        
    }];
}

#pragma mark 保存当班金额记录(GetMoneyOnDuty)
-(void)saveMoneyOnDuty:(NSDictionary *)dic{
    //将上述数据全部存储到NSUserDefaults中
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //
    [userDefaults setObject:[dic objectForKey:kCashSale] forKey:kCashSale];
    [userDefaults setObject:[dic objectForKey:kCardSale] forKey:kCardSale];
    [userDefaults setObject:[dic objectForKey:kVoucherSale] forKey:kVoucherSale];
    [userDefaults setObject:[dic objectForKey:kVoucher2Sale] forKey:kVoucher2Sale];
    [userDefaults setObject:[dic objectForKey:kOtherSale] forKey:kOtherSale];
    [userDefaults setObject:[dic objectForKey:kSumTotal] forKey:kSumTotal];
    
    MBLog(@"%@ %d %d",[dic objectForKey:kCashCode],[[dic objectForKey:kCashSale] intValue],[[dic objectForKey:kCashSale] intValue]);
    [userDefaults synchronize];
    
}

@end
